package pl.edu.mimuw.irs.rcp.wizard;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

import pl.edu.mimuw.irs.core.model.design.IRSWorkflow;
import pl.edu.mimuw.irs.core.util.persistence.IPersistenceManager;
import pl.edu.mimuw.irs.core.util.persistence.PersistenceAdapter;
import pl.edu.mimuw.irs.rcp.controller.editor.IRSWorkflowEditor;
import pl.edu.mimuw.irs.rcp.util.Logger;
import pl.edu.mimuw.irs.rcp.util.MessageCode;
import pl.edu.mimuw.irs.rcp.util.UITexts;
import pl.edu.mimuw.irs.rcp.wizard.newproject.ProjectModel;
import pl.edu.mimuw.irs.rcp.wizard.newproject.ProjectPage;

public class NewProjectWizard extends Wizard implements INewWizard {
	
	// *** Attributes

	//private IWorkbench workbench;
	//private IStructuredSelection selection;
	private ProjectPage projectPage;
	//private ResourcesPage resourcesPage;
	private ProjectModel newProjectModel;
	public ProjectModel getNewProjectModel() {
		return newProjectModel;
	}

	// *** Methods
	
	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.newProjectModel = new ProjectModel();
		//this.workbench = workbench;
		//this.selection = selection;
		
		this.setWindowTitle(UITexts.get(MessageCode.PROJECT_WIZARD_NAME));
	}
	
	@Override
	public IWizardPage getStartingPage() {
		return projectPage;
	}
	
	@Override
	public void addPages() {
		 projectPage = new ProjectPage();
	     addPage(projectPage);
		 //resourcesPage = new ResourcesPage();
	     //addPage(resourcesPage);
	}
	
	@Override
	public boolean canFinish() {
		return projectPage.isPageComplete();
	}
	
	
	
	@Override
	public boolean performFinish()  {

		InputStream source = createWorkflowAsStream();
		IProject project = createProject();
		if (source == null || project == null) return false;
		
		IFile file = null;
		
		try {
		    file = project.getFile("workflow.irs");
		    file.create(source, IResource.FORCE, null);
		} catch (CoreException e1) {
			Logger.handle(MessageCode.WORKFLOW_SAVE_FILE_PROBLEM, e1);
			return false;
		}
		
		FileEditorInput fileEditorInput = new FileEditorInput(file);
		IWorkbench workbench = PlatformUI.getWorkbench();
		//IEditorDescriptor desc = workbench.getEditorRegistry().getDefaultEditor(file.getName());
		IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
		//page.openEditor(fileEditorInput, desc.getId());
		try {
			page.openEditor(fileEditorInput, IRSWorkflowEditor.ID);
		} catch (PartInitException e) {
			Logger.handle(MessageCode.EDITOR_OPEN_PROBLEM, e, fileEditorInput.getName());
			return false;
		}
		
		return true;
	}
	
	private IProject createProject() {
		IWorkspaceRoot rootWorkspace = ResourcesPlugin.getWorkspace().getRoot();
		IProject project = rootWorkspace.getProject(newProjectModel.projectName);
		
		try {
			project.create(new NullProgressMonitor());
			project.open(new NullProgressMonitor());
		} catch (CoreException e1) {
			Logger.handle(MessageCode.WORKFLOW_CREATE_PROJECT_PROBLEM, e1);
			return null;
		}
		
		return project;
	}
	
	public InputStream createWorkflowAsStream() {
		IRSWorkflow workflow = new IRSWorkflow();
		workflow.createSampleModel();
		IPersistenceManager manager = PersistenceAdapter.getManager(workflow.getClass());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		try {
			manager.writeToStream(workflow, baos);
		} catch (Exception ex) {
			Logger.handle(MessageCode.WORKFLOW_SAVE_SERIALIZE_PROBLEM, ex);
			return null;
		}
		
		InputStream stream = new ByteArrayInputStream(baos.toByteArray());
		return stream;
	}
}
